﻿namespace ImageDupFinder
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>
    /// Represents a two-state Windows button control.
    /// </summary>
    public class ToggleButton : Button
    {
        /// <summary>
        /// Toggle state.
        /// </summary>
        private bool toggled = false;

        /// <summary>
        /// Image set after button is toggled.
        /// </summary>
        private Image toggledImage;

        /// <summary>
        /// Test set after button is toggled.
        /// </summary>
        private string toggledText;

        /// <summary>
        /// Initializes a new instance of the ToggleButton class.
        /// </summary>
        public ToggleButton()
            : base()
        {
        }

        /// <summary>
        /// Represents the method that handles a button toggled event. 
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="toggled">new toggle state</param>
        public delegate void ToggleChangeHandler(object sender, bool toggled);

        /// <summary>
        /// Occur when button is toggled
        /// </summary>
        [Category("Behavior"), Browsable(true)]
        public event ToggleChangeHandler ToggleStateChanged;

        /// <summary>
        /// Gets or sets a value indicating whether button is toogled.
        /// </summary>
        [Category("Behavior"), Browsable(true), DefaultValue(false)]
        public bool Toggled
        {
            get
            {
                return toggled;
            }

            set
            {
                if (toggled != value)
                {
                    toggled = value;
                    OnToggle();
                }
            }
        }

        /// <summary>
        /// Gets or sets button toggle image.
        /// </summary>
        [Category("Appearance"), Browsable(true), DefaultValue(null)]
        public Image ToggledImage
        {
            get { return toggledImage; }
            set { toggledImage = value; }
        }

        /// <summary>
        /// Gets or sets button toggle text.
        /// </summary>
        [Category("Appearance"), Browsable(true), DefaultValue("")]
        public string ToggledText
        {
            get { return toggledText; }
            set { toggledText = value; }
        }

        /// <summary>
        /// Set toggle state without calling toggle event
        /// </summary>
        /// <param name="state">new toggle state</param>
        public void SetToggleState(bool state)
        {
            if (toggled != state)
            {
                toggled = state;
                ToggleProperties();
            }
        }

        /// <summary>
        /// Changes properties to thier toggle values.
        /// </summary>
        protected virtual void ToggleProperties()
        {
            string old = this.Text;
            this.Text = toggledText;
            toggledText = old;

            Image oldImg = this.Image;
            this.Image = toggledImage;
            toggledImage = oldImg;
        }

        /// <summary>
        /// Raises the ToggleStateChanged event.
        /// </summary>
        protected virtual void OnToggle()
        {
            ToggleProperties();

            ToggleChangeHandler tmp = this.ToggleStateChanged;
            if (tmp != null)
            {
                ToggleStateChanged(this, toggled);
            }
        }

        /// <summary>
        /// Raises Click event.
        /// Sets toggle state.
        /// </summary>
        /// <param name="e">Event args</param>
        protected override void OnClick(EventArgs e)
        {
            Toggled = !toggled;
            base.OnClick(e);
        }
    }
}
